{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pickle\n",
    "import torch\n",
    "import numpy as np\n",
    "from math import ceil\n",
    "from model_vc import Generator\n",
    "\n",
    "\n",
    "def pad_seq(x, base=32):\n",
    "    len_out = int(base * ceil(float(x.shape[0])/base))\n",
    "    len_pad = len_out - x.shape[0]\n",
    "    assert len_pad >= 0\n",
    "    return np.pad(x, ((0,len_pad),(0,0)), 'constant'), len_pad\n",
    "\n",
    "device = 'cuda:0'\n",
    "G = Generator(32,256,512,32).eval().to(device)\n",
    "\n",
    "g_checkpoint = torch.load('autovc.ckpt')\n",
    "G.load_state_dict(g_checkpoint['model'])\n",
    "\n",
    "metadata = pickle.load(open('metadata.pkl', \"rb\"))\n",
    "\n",
    "spect_vc = []\n",
    "\n",
    "for sbmt_i in metadata:\n",
    "             \n",
    "    x_org = sbmt_i[2]\n",
    "    x_org, len_pad = pad_seq(x_org)\n",
    "    uttr_org = torch.from_numpy(x_org[np.newaxis, :, :]).to(device)\n",
    "    emb_org = torch.from_numpy(sbmt_i[1][np.newaxis, :]).to(device)\n",
    "    \n",
    "    for sbmt_j in metadata:\n",
    "                   \n",
    "        emb_trg = torch.from_numpy(sbmt_j[1][np.newaxis, :]).to(device)\n",
    "        \n",
    "        with torch.no_grad():\n",
    "            _, x_identic_psnt, _ = G(uttr_org, emb_org, emb_trg)\n",
    "            \n",
    "        if len_pad == 0:\n",
    "            uttr_trg = x_identic_psnt[0, 0, :, :].cpu().numpy()\n",
    "        else:\n",
    "            uttr_trg = x_identic_psnt[0, 0, :-len_pad, :].cpu().numpy()\n",
    "        \n",
    "        spect_vc.append( ('{}x{}'.format(sbmt_i[0], sbmt_j[0]), uttr_trg) )\n",
    "        \n",
    "        \n",
    "with open('results.pkl', 'wb') as handle:\n",
    "    pickle.dump(spect_vc, handle)          "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
